import 'dart:async';
import 'package:flutter_douban/main_page/tabbar_page.dart';
import 'package:flutter/material.dart';

class TransitPage extends StatefulWidget {
  const TransitPage({Key? key}) : super(key: key);

  @override
  _TransitPageState createState() => _TransitPageState();
}

class _TransitPageState extends State<TransitPage> {
  int _currentTime = 6;
  Timer? _timer;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 开始定时器,一秒刷新一次
    _timer = Timer.periodic(Duration(milliseconds: 1000), (timer) {
      setState(() {
        _currentTime--;
      });
      if (_currentTime <= 0) {
        _jumpBootPage();
      }
    });
  }

  // 跳转首页方法
  void _jumpBootPage() {
    _timer!.cancel(); // 取消定时器
    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
      builder: (BuildContext context) {
        return TabbarPage();
      },
    ), (route) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Image.asset(
            'assets/images/common/LaunchImage.png',
            fit: BoxFit.fill, // 充满模式
            width: MediaQuery.of(context).size.width, // 屏幕宽度
            height: MediaQuery.of(context).size.height, // 屏幕高度
          ),
          Positioned(
            // 叠加布局
            top: MediaQuery.of(context).padding.top, //手机状态栏高度
            right: 10,
            child: Row(
              children: [
                Container(
                  alignment: Alignment.center,
                  width: 60,
                  height: 30,
                  decoration: BoxDecoration(
                    color: Colors.black.withOpacity(0.6),
                    borderRadius: BorderRadius.circular(15),
                  ),
                  child: Text(
                    "广告",
                    style: TextStyle(color: Colors.white, fontSize: 12),
                  ),
                ),
                SizedBox(width: 10,),
                InkWell(
                  child: _cilpButton(),
                  onTap: _jumpBootPage,
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _cilpButton() {
    return Container(
      width: 60,
      height: 30,
      decoration: BoxDecoration(
        color: Colors.black.withOpacity(0.6),
        borderRadius: BorderRadius.circular(15),
    ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center, //居中显示
        // 纵向布局
        children: [
          Text(
            "${_currentTime}s",
            style: TextStyle(color: Colors.white, fontSize: 12),
          ),
          Text(
            " 跳过",
            style: TextStyle(color: Colors.white, fontSize: 12),
          ),
        ],
      ),
    );
  }
}
